/**
 * Created by DELL on 2017/6/7.
 */

import React  from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TextInput,
    Image,
    PixelRatio,
    TouchableHighlight,
    TouchableOpacity,
    ListView,
    FlatList,
    RefreshControl,

} from 'react-native';

import BaseComponent, {base,AppIndictor,AppCustomDialog} from '../../base/Base' ;


let {
    N034_USERS$_GET_ADDRESS,//获取用户地址
} = base.requests;
export default class Address extends BaseComponent {
    static navigationOptions = ({navigation}) => ({
        title:'地址管理',//navigation 的默认设置属性 标题
    })

    constructor(props) {
        super(props);
        this.state={
            isFromOrder: false,
            showIndictor:true,
            headLoading:false,
            appDialogVisible:false,
            data:null,
        }


    }

    componentWillMount() {

    }

    componentDidMount(){


        this._getAddressList();

        let params = this.props.navigation.state.params;

        if (params && params.isFromOrder) {
            this.setState({
                isFromOrder: true,
            })
        }
    }


    _getAddressList() {
        base.network.getUipData(N034_USERS$_GET_ADDRESS, {}, (success) => {
            this.setState({
                data:success,
            })
            this._setAppIndictor(false)
        }, (error) => {
            this._setAppIndictor(false)
        })
    }


    _setAppDialogVisible(visible){
        this.setState({
            appDialogVisible:visible,
        })
    }
    _setOnRefreshing(visible){
        this.setState({
            headLoading:visible,
        })
    }

    _onAddressPress=(itemData)=> {
        let {navigate,goBack,state}=this.props.navigation;
        if(this.state.isFromOrder){
            state.params.callback(itemData);
            goBack();
        }else {
            base.toastShort(itemData.cellPhone)
        }

    }

    _onAddressLongPress=(item)=>{
        this._setAppDialogVisible(true)
    }

    _setAppIndictor(visible){
        this.setState({
            showIndictor:visible,
        })
    }

    setListView() {
        let bbbb=this.state.addressDatas;

        if(bbbb){
            return (
            <View style={{backgroundColor:'#fff'}}>
                <ListView
                    contentContainerStyle={{}}
                    enableEmptySections={true}
                    dataSource={bbbb}
                    renderRow={(rowData) => {
                        return <AddressItem
                            dataSource={rowData}
                            onPress={(itemData) => {
                                this._onAddressPress(itemData);
                            }}
                        />
                    }}
                    stickySectionHeadersEnabled={true}
                />
            </View>

            )
        }else {

        }

    }

    _flatListHeader=()=>{
        return <Text>我是头部</Text>
    }
    _flatListFooter=()=>{
        return <Text>我是尾部</Text>
    }
    _flatKeyExtractor=(item,index)=>{
        return item.id;
    }

    _flatRenderItem=({item})=>{
        return(
            <AddressItem
                dataSource={item}
                onPress={this._onAddressPress}
                onLongPress={this._onAddressLongPress}
            />
        )
    }
    _onRefresh=()=>{
        this._setOnRefreshing(true)
        base.network.getUipData(N034_USERS$_GET_ADDRESS, {}, (success) => {
            this.setState({
                data:success,
            })
            this._setOnRefreshing(false)
        }, (error) => {
            this._setOnRefreshing(false)
        })
    }
    _onEndReached(){
        base.toastShort('加载更多')
    }
    setListView_222() {
        let bbbb=this.state.data;
        if(bbbb){
            return (
                <View style={{flex:1}}>
                    <FlatList
                        style={{flex:1}}
                        refreshing={this.state.headLoading}
                        onRefresh={this._onRefresh}
                        data={bbbb}
                        keyExtractor={this._flatKeyExtractor}
                        renderItem={this._flatRenderItem}
                        onEndReached={this._onEndReached}
                        onEndReachedThreshold={0.5}
                    />
                </View>
            )
        }

    }
    setBottomView(){
        return(
            <TouchableOpacity>
                <View style={{backgroundColor:'#ff6138',alignItems:'center',justifyContent:'center',paddingTop:10,paddingBottom:10}}>
                    <Text style={{fontSize:18,color:'#fff'}}>新增地址</Text>
                </View>
            </TouchableOpacity>
        )
    }

    render() {
        return (

            <View style={{flex:1}}>
                <AppIndictor
                    modalVisible={this.state.showIndictor}
                />

                <AppCustomDialog
                    modalVisible={this.state.appDialogVisible}
                    onLeftPress={()=>{this._setAppDialogVisible(false)}}
                    onRightPress={()=>{this._setAppDialogVisible(false)}}
                    dialogTitle={'删除提醒'}
                    dialogMessage={'是否忍心删除当前选择的地址...'}
                    dialogOnLeftTitle={'狠下心'}
                    dialogOnRightTitle={'不忍心哟'}


                />

                {this.setListView_222()}
                {this.setBottomView()}

            </View>
        );


    };


}

//热销优品 自定义组件
class AddressItem extends BaseComponent {

    render() {
        let data = this.props.dataSource;

        return (
            <TouchableHighlight
                onLongPress={this.props.onLongPress.bind(this, data)}
                onPress={this.props.onPress.bind(this, data)}
                underlayColor='#00000000'
            >
                <View style={{paddingLeft:15,paddingRight:15,paddingTop:15,borderRadius:5,marginTop:15,marginRight:15,marginLeft:15,backgroundColor:'#fff'}}>
                    <View style={{flexDirection: 'row',flex:1,}}>
                        <Text>{data.name}</Text>
                        <Text>   {data.cellPhone}</Text>

                    </View>

                    <Text style={{marginTop:5}}>{data.province + data.city + data.district + data.details}</Text>

                    <View style={{backgroundColor:'#f5f5f5',height:1,marginTop:15}}/>
                </View>
            </TouchableHighlight>
        )
    }

}